﻿using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using UniTAC.Properties;

namespace UniTAC
{
    public partial class AutoCompleteMenu
    {
        #region Methods

        /// <summary>
        /// This function will adjust Tooltip position and
        /// will keep it always inside the screen area.
        /// </summary>
        private void AdjustUI()
        {
            // Get Current Screen Resolution
            Rectangle workingArea = SystemInformation.WorkingArea;

            // If current caret position throws Tooltip outside of screen area
            // then do some UI adjustment.
            if (caretPosition.X + this.Width > workingArea.Width)
            {
                caretPosition.X = caretPosition.X - this.Width;
            }

            if (caretPosition.Y + this.Height > workingArea.Height)
            {
                caretPosition.Y = caretPosition.Y - this.Height - 22;
            }

            this.Left = caretPosition.X;
            this.Top = caretPosition.Y;
        }

        /// <summary>
        /// Evaluates Cursor Position with respect to client screen.
        /// </summary>
        private void EvaluateCaretPosition()
        {

            // Fetch GUITHREADINFO
            GetCaretPosition();

            caretPosition.X = (int)guiInfo.rcCaret.Left;
            caretPosition.Y = (int)guiInfo.rcCaret.Bottom + 5;

            posX = caretPosition.X;
            posY = caretPosition.Y;

            ClientToScreen(guiInfo.hwndCaret, out caretPosition);
        }

        /// <summary>
        /// Get the caret position
        /// </summary>
        public void GetCaretPosition()
        {
            guiInfo = new GUITHREADINFO();
            guiInfo.cbSize = (uint)Marshal.SizeOf(guiInfo);

            // Get GuiThreadInfo into guiInfo
            GetGUIThreadInfo(0, out guiInfo);
        }

        private void InsertWord()
        {
            string word = listBox1.SelectedItem.ToString().Substring(label2.Text.Length);
            try
            {
                if (word.Length > 0)
                    SendKeys.SendWait(word);
            }
            catch (System.NullReferenceException)
            { }
            label2.Text = "";
        }

        #endregion
    }
}
